﻿@using Think9.Models;
@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }

<fieldset class="table-fieldset" id="searchfield" style="display:none">
    <legend>搜索信息</legend>
    <div style="margin: 5px 5px 5px 5px">
        <form class="layui-form" action="">
            <div class="layui-inline">
                <select id="DeptNo" name="DeptNo">
                    <option value="">==请选择==</option>
                    @foreach (valueTextEntity item in ((IEnumerable<valueTextEntity>)ViewBag.SelectList))
                    {
                    <option value="@item.Value">@item.Text</option>
}
                </select>
            </div>
            <div class="layui-inline">
                <input class="layui-input" placeholder="关键字" name="key" id="key" autocomplete="off">
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn layui-btn-primary" id="search"><i class="layui-icon layui-icon-search"></i></button>
                <button type='reset' class='layui-btn layui-btn-primary'><i class='layui-icon layui-icon-refresh'></i></button>
            </div>
        </form>
    </div>
</fieldset>
<!--数据表格-->
<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
<script>
    layui.use(["table", "form", "exLayer", "exUtils", "miniPage"], function () {
        let table = layui.table;
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let miniPage = layui.miniPage;

        var openWH = miniPage.getOpenWidthHeight();
        let $ = layui.$;

        let ThisTable = table.render({
            elem: "#tableId",
            url: "/SysBasic/User/GetPageListBySearch?key=" + $("#key").val() + "&dep=" + $("#DeptNo").val(),
            limits: [10, 50, 100],
            limit: 10,
            method: "POST",
            page: true,
            defaultToolbar: [{ title: '搜索', layEvent: 'searchShow', icon: 'layui-icon-search' }, 'filter', 'exports'],
            toolbar: "#toolbarTpl",
            cols: [[
                { type: "checkbox" },
                { field: "Account", title: "账户", sort: true },
                { field: "RealName", title: "姓名", sort: true },
                { field: "DepartmentName", title: "部门", width: 200, sort: true },
                { field: "RoleName", title: "角色", width: 150, sort: true },
                { field: "EnabledMark", title: "状态", width: 80, templet: "#enabledMarkTpl", sort: true },
                { title: "操作", width: 200, hight: 24, align: "center", fixed: "right", templet: "#operationTpl" }
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count);
            }
        });

        form.on("submit(search)", function (data) {
            ThisTable.reload({
                where: data.field,
                page: { curr: 1 }
            });
            return false;
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
                case "batchDel":
                    batchDel();
                    break;
                case "searchShow":
                    searchShow();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.Id);//field Id 和 数据库表字段 Id 要一致
                    break;
                case "del":
                    del(data.Id);
                    break;
                case "pwdReset":
                    pwdReset(data.Id);
                    break;
            }
        });

        function searchShow() {
            var display = $('#searchfield').css('display');
            if (display == 'none') {
                $("#searchfield").show();
            }
            else {
                $("#searchfield").hide();
            }
        }

        //查询
        $("#search").click(function () {
            search();
        });

        function add() {
            exLayer.open("添加用户", "/SysBasic/User/Add", openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        }

        function batchDel() {
            exLayer.confirm("确定要批量删除吗？", function (index) {
                layer.close(index);
                let idsStr = exUtils.tableBatchCheck(table);
                if (idsStr) {
                    exUtils.ajax("/SysBasic/User/BatchDel", "get", { idsStr: idsStr }, true).done(function (response) {
                        exUtils.tableSuccessMsg(response.message);
                        $(".layui-laypage-btn")[0].click();
                    }).fail(function (error) {
                        console.log(error);
                    });
                }
            });
        }

        function edit(id) {
            exLayer.open("编辑用户", "/SysBasic/User/Edit/" + id, openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        }

        function del(id) {
            exLayer.confirm("确定要删除吗？", function () {
                exUtils.ajax("/SysBasic/User/Delete", "get", { id: id }, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                    $(".layui-laypage-btn")[0].click();
                }).fail(function (error) {
                    console.log(error);
                });
            })
        }

        function pwdReset(id) {
            exLayer.confirm("将用户密码重置为123456，确定要重置密码吗？", function () {
                exUtils.ajax("/SysBasic/User/InitPwd", "get", { id: id }, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                }).fail(function (error) {
                    console.log(error);
                });
            })
        }

        function search() {
            $("#searchfield").hide();
            ThisTable.reload({
                where: { key: $("#key").val(), dep: $("#DeptNo").val() },
                page: { curr: 1 }
            });
            return false;
        }
    })
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <button class='layui-btn layui-btn-sm layui-btn-normal ' lay-event='add' id='add'><i class="fa fa-plus"></i>添加用户</button>
    <button class='layui-btn layui-btn-sm layui-btn-danger' lay-event='batchDel' id='batchDel'><i class="fa fa-remove"></i>批量删除</button>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a class='layui-btn layui-btn-normal layui-btn-xs ' href='javascript:;' lay-event='edit' id='edit'>编辑</a>
    <a class='layui-btn layui-btn-xs layui-btn-danger' href='javascript:;' lay-event='del' id='del'>删除</a>
    <a class='layui-btn layui-btn-xs layui-btn-warm' href='javascript:;' lay-event='pwdReset' id='pwdReset'>重置密码</a>
</script>
<!-- 启用|停用模板 -->
<script type="text/html" id="enabledMarkTpl">
    {{#  if(!d.EnabledMark){ }}
    <span class="layui-btn layui-btn-normal layui-btn-xs">已启用</span>
    {{#  } else{ }}
    <span class="layui-btn layui-btn-warm layui-btn-xs">已停用</span>
    {{#  } }}
</script>
<!-- 性别模板 -->
<script type="text/html" id="genderTpl">
    {{#  if(d.Gender){ }}

    <span>男</span>
    {{#  } else{ }}

    <span>女</span>
    {{#  } }}
</script>